<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */
namespace api\admin\controller;

use app\admin\model\UserModel;
use api\admin\controller\MyController;

/**
 * Description of MetaMyController
 *
 * @author PC
 */
class MetaMyController extends MyController
{
    
    /**
     * 编辑当前管理员个人信息
     * @throws \think\exception\DbException
     * @OA\Put(
     *     tags={"admin"},
     *     path="/admin/MetaMy/editMyInfo",
     *     summary="编辑当前管理员个人信息",
     *     description="编辑当前管理员个人信息",
     *     @OA\RequestBody(
     *         description="请求参数",
     *         @OA\MediaType(
     *             mediaType="application/x-www-form-urlencoded",
     *             @OA\Schema(ref="#/components/schemas/AdminMyInfoPutRequest")
     *         ),
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(ref="#/components/schemas/AdminMyInfoPutRequest")
     *         )
     *     ),
     *     @OA\Response(
     *          response="1",
     *          description="success",
     *          @OA\JsonContent(example={"code": 1,"msg": "保存成功","data":""})
     *     ),
     *     @OA\Response(
     *          response="0",
     *          @OA\JsonContent(example={"code": 0,"msg": "error!","data":""})
     *     ),
     * )
     */
    public function editMyInfo()
    {
        $data = $this->request->param();
        // user_nickname不能为空
        if (empty($data['user_nickname'])) {
            $this->error('昵称不能为空！');
        }
        $data['birthday'] = strtotime($data['birthday']);
        $data['avatar'] = $data['avatar']['avatar'];
        // 判断user_nickname是否重复
        $user = UserModel::where('user_nickname', $data['user_nickname'])->find();
        if (!empty($user)) {
            $this->error('昵称已存在！');
        }
        $userId = $this->getUserId();
        $user = UserModel::find($userId);
        if (empty($user)) {
            $this->error('未找到用户！');
        }
        $user->allowField(['user_nickname', 'sex', 'birthday', 'user_url', 'signature', 'avatar'])->save($data);
        $this->success(lang('EDIT_SUCCESS'));
    }

}
